// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.PurchasePlans;
import com.fowo.api.model.purchase.plans.PurchasePlansDetailedInfoParam;
import com.fowo.api.model.purchase.plans.PurchasePlansImportPo;
import com.fowo.api.model.purchase.plans.PurchasePlansItemVo;
import com.fowo.api.model.purchase.plans.PurchasePlansSearchParamPo;
import com.fowo.api.model.purchase.plans.PurchasePlansVo;
import com.fowo.api.service.PurchasePlansService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "采购计划")
@RequestMapping("/api/purchase-plans")
@RestController
public class PurchasePlansController {

  @Resource
  private PurchasePlansService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<PurchasePlansItemVo>> search(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<PurchasePlansItemVo>>> searchOptions(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<PurchasePlansItemVo>> getPage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<PurchasePlansVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取创建批次号相关的编号列表")
  @GetMapping("ppg-sn/{texts}/ids")
  public R<List<Long>> getIdsByPpgSn(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByPpgSn(nameList));
  }

  @Operation(summary = "获取编号相关的创建批次号列表")
  @GetMapping("ppg-sn/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getPpgSnByIds(idList));
  }

  @PreAuthorize("@am.canAccess('purchasePlansCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody PurchasePlansVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('purchasePlansEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody PurchasePlansVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('purchasePlansEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody PurchasePlansVo model) {
    return R.run(() -> service.updateForEdit(model));
  }


  @Operation(summary = "获取店铺下拉框")
  @GetMapping("getShopList")
  public R getShopList( @RequestParam(name = "fnsku")String fnsku) {
    return R.data(() -> service.getShopList(fnsku));
  }

  @PreAuthorize("@am.canAccess('purchasePlansDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('purchasePlansDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('purchasePlansImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<PurchasePlansImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('purchasePlansImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<PurchasePlansImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "采购计划导入模版.xlsx");
    InputStream inputStream = new ClassPathResource(
      "templates/PurchasePlans.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('purchasePlansExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    PurchasePlansSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('purchasePlansCancellation')")
  @Operation(summary = "作废")
  @PostMapping("cancellation")
  public R cancellation(@RequestBody IdListPo po) {
    return R.run(() -> service.cancellation(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('purchasePlansPurchasingPlanBoard')")
  @Operation(summary = "采购计划看板")
  @PostMapping("purchasingPlanBoard")
  public R purchasingPlanBoard(@RequestBody IdListPo po) {
    return R.run(() -> service.purchasingPlanBoard(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('purchasePlansSyncPurchasePlans')")
  @Operation(summary = "拉取采购领星计划")
  @PostMapping("syncPurchasePlans")
  public R syncPurchasePlans() {
    return R.run(() -> service.syncPurchasePlans());
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "全部")
  @GetMapping("complete")
  public R<List<PurchasePlansItemVo>> complete(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.complete(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("completePage")
  public R<Page<PurchasePlansItemVo>> completePage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completePage(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("completePage-with-child")
  public R<Page<?>> completePageWithChild(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.completePageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("completeLastUpdateTime")
  public R<Date> completeLastUpdateTime(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completeLastUpdateTime(search));
  }

  @Operation(summary = "待提交")
  @GetMapping("waiting-subm")
  public R<List<PurchasePlansItemVo>> waitingSubm(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingSubm(search));
  }

  @Operation(summary = "待提交分页")
  @GetMapping("waiting-submPage")
  public R<Page<PurchasePlansItemVo>> waitingSubmPage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingSubmPage(search));
  }

  @Operation(summary = "待提交包括子表的查询结果")
  @GetMapping("waiting-submPage-with-child")
  public R<Page<?>> waitingSubmPageWithChild(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.waitingSubmPageWithChildSearch(search));
  }

  @Operation(summary = "待提交最后更新时间")
  @GetMapping("waiting-submLastUpdateTime")
  public R<Date> waitingSubmLastUpdateTime(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingSubmLastUpdateTime(search));
  }

  @Operation(summary = "待审批")
  @GetMapping("theapprovalispending")
  public R<List<PurchasePlansItemVo>> theapprovalispending(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.theapprovalispending(search));
  }

  @Operation(summary = "待审批分页")
  @GetMapping("theapprovalispendingPage")
  public R<Page<PurchasePlansItemVo>> theapprovalispendingPage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.theapprovalispendingPage(search));
  }

  @Operation(summary = "待审批包括子表的查询结果")
  @GetMapping("theapprovalispendingPage-with-child")
  public R<Page<?>> theapprovalispendingPageWithChild(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.theapprovalispendingPageWithChildSearch(search));
  }

  @Operation(summary = "待审批最后更新时间")
  @GetMapping("theapprovalispendingLastUpdateTime")
  public R<Date> theapprovalispendingLastUpdateTime(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.theapprovalispendingLastUpdateTime(search));
  }

  @Operation(summary = "待采购")
  @GetMapping("tobepurchased")
  public R<List<PurchasePlansItemVo>> tobepurchased(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.tobepurchased(search));
  }

  @Operation(summary = "待采购分页")
  @GetMapping("tobepurchasedPage")
  public R<Page<PurchasePlansItemVo>> tobepurchasedPage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.tobepurchasedPage(search));
  }

  @Operation(summary = "待采购包括子表的查询结果")
  @GetMapping("tobepurchasedPage-with-child")
  public R<Page<?>> tobepurchasedPageWithChild(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.tobepurchasedPageWithChildSearch(search));
  }

  @Operation(summary = "待采购最后更新时间")
  @GetMapping("tobepurchasedLastUpdateTime")
  public R<Date> tobepurchasedLastUpdateTime(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.tobepurchasedLastUpdateTime(search));
  }

  @Operation(summary = "已完成")
  @GetMapping("completed")
  public R<List<PurchasePlansItemVo>> completed(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completed(search));
  }

  @Operation(summary = "已完成分页")
  @GetMapping("completedPage")
  public R<Page<PurchasePlansItemVo>> completedPage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedPage(search));
  }

  @Operation(summary = "已完成包括子表的查询结果")
  @GetMapping("completedPage-with-child")
  public R<Page<?>> completedPageWithChild(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.completedPageWithChildSearch(search));
  }

  @Operation(summary = "已完成最后更新时间")
  @GetMapping("completedLastUpdateTime")
  public R<Date> completedLastUpdateTime(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedLastUpdateTime(search));
  }

  @Operation(summary = "已驳回")
  @GetMapping("rejected")
  public R<List<PurchasePlansItemVo>> rejected(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.rejected(search));
  }

  @Operation(summary = "已驳回分页")
  @GetMapping("rejectedPage")
  public R<Page<PurchasePlansItemVo>> rejectedPage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.rejectedPage(search));
  }

  @Operation(summary = "已驳回包括子表的查询结果")
  @GetMapping("rejectedPage-with-child")
  public R<Page<?>> rejectedPageWithChild(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.rejectedPageWithChildSearch(search));
  }

  @Operation(summary = "已驳回最后更新时间")
  @GetMapping("rejectedLastUpdateTime")
  public R<Date> rejectedLastUpdateTime(PurchasePlansSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.rejectedLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("havebeenvoided")
  public R<List<PurchasePlansItemVo>> havebeenvoided(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.havebeenvoided(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("havebeenvoidedPage")
  public R<Page<PurchasePlansItemVo>> havebeenvoidedPage(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.havebeenvoidedPage(search));
  }

  @Operation(summary = "已作废包括子表的查询结果")
  @GetMapping("havebeenvoidedPage-with-child")
  public R<Page<?>> havebeenvoidedPageWithChild(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.havebeenvoidedPageWithChildSearch(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("havebeenvoidedLastUpdateTime")
  public R<Date> havebeenvoidedLastUpdateTime(
    PurchasePlansSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.havebeenvoidedLastUpdateTime(search));
  }

  @Operation(summary = "获取采购计划生成")
  @PostMapping("DetailedInfo")
  public R detailedInfo(PurchasePlansDetailedInfoParam param) {
    return R.run(() -> service.detailedInfo(param));
  }
}
